package com.neuedu;

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import com.opencsv.exceptions.CsvValidationException;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ProductionSalesClean {

    public static void main(String[] args) {
        String inputFilePath = "C:\\Users\\29874\\Desktop\\VehicleProductionSales.csv";
        String outputFilePath = "D:\\B_HtuLearn\\Data\\VehicleProductionAndSales.csv";

        // 日期格式转换
        SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy年M月");  // 输入格式 "2021年7月"
        SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd");  // 输出格式 "2021-07-01"

        try (CSVReader reader = new CSVReader(new FileReader(inputFilePath));
             CSVWriter writer = new CSVWriter(new FileWriter(outputFilePath),
                     CSVWriter.DEFAULT_SEPARATOR,
                     CSVWriter.NO_QUOTE_CHARACTER,
                     CSVWriter.DEFAULT_ESCAPE_CHARACTER,
                     CSVWriter.DEFAULT_LINE_END)) {

            // 读取CSV数据
            String[] header = reader.readNext();
            if (header != null) {
                writer.writeNext(header);  // 写入头部
            } else {
                System.out.println("Error: No header found.");
                return;
            }

            String[] nextLine;
            while ((nextLine = reader.readNext()) != null) {
                if (nextLine.length > 1) {
                    // 处理第一列日期格式
                    try {
                        // 如果第一列是有效日期格式，进行转换
                        String originalDate = nextLine[0];
                        Date date = inputDateFormat.parse(originalDate);
                        String formattedDate = outputDateFormat.format(date);
                        nextLine[0] = formattedDate;  // 替换为标准的日期格式
                    } catch (ParseException e) {
                        System.out.println("Skipping invalid date format in first column: " + nextLine[0]);
                    }

                    // 处理其他数据列（转换为辆）
                    for (int i = 1; i < nextLine.length; i++) { // 跳过第一列
                        try {
                            // 数据从万辆转换为辆
                            nextLine[i] = String.valueOf(Math.round(Double.parseDouble(nextLine[i]) * 10000));
                        } catch (NumberFormatException e) {
                            System.out.println("Skipping non-numeric value: " + nextLine[i]);
                        }
                    }
                    writer.writeNext(nextLine);  // 写入处理后的行
                } else {
                    System.out.println("Skipping invalid row: " + String.join(",", nextLine));
                }
            }

        } catch (IOException | CsvValidationException e) {
            System.out.println("Error processing CSV file: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

